
layui.use(['form','layer','layedit','laydate','upload','table'],function(){
    $ = layui.jquery;
    var layer = layui.layer ,table = layui.table,laydate = layui.laydate;
    //第一个实例
    var indexx = table.render({
        elem: '#demo'
        ,id:'testReload'
        ,height: "auto"
        ,method:"post"
        ,url: baseUrl+'gametea/getrooms' //数据接口
        ,request: {
            pageName: 'pageNum' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        ,page: false //开启分页
        ,cols: [[ //表头
            {field:"roomName", title: '房间号',align:'center'}
            ,{field:"roomType", title: '房间模式',align:'center',templet:function (d) {
                    if(d.roomType == 1){
                        return "<span style='color: #1E9FFF'>明牌抢庄</span>";
                    }
                }}
            ,{field:"room", title: '房间等级',align:'center',templet:function (d) {
                    if(d.room == 1){
                        return "<span style='color: #1E9FFF'>体验</span>";
                    }
                    if(d.room == 2){
                        return "<span style='color: #1E9FFF'>初级</span>";
                    }
                    if(d.room == 3){
                        return "<span style='color: #1E9FFF'>中级</span>";
                    }
                    if(d.room == 4){
                        return "<span style='color: #1E9FFF'>高级</span>";
                    }
                }}
            ,{field: "small", title: '最小带入',align:'center'}
            ,{field: "tea", title: '茶水费比例',align:'center',templet:function (d) {
                    if(d.tea == 1){
                        return "<span style='color: #1E9FFF'>20%</span>";
                    }
                    if(d.tea == 2){
                        return "<span style='color: #1E9FFF'>30%</span>";
                    }
                    if(d.tea == 3){
                        return "<span style='color: #1E9FFF'>40%</span>";
                    }
                }}
            ,{field: "personnum", title: '人数',align:'center'}
            ,{field:"points" , title: '底分',align:'center'}
            ,{field:"double" , title: '闲家推注',align:'center'}
            // ,{fixed: 'right',   title: '操作', width:150, align:'center', toolbar: '#barDemo'}
        ]]

    });
    //监听工具条
    table.on('tool(test)', function(obj){
        var data = obj.data;
        var layEvent = obj.event;
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if(layEvent === 'del') { //编辑
            layer.confirm('真的删除行么', function(index){
                layer.close(index);
                //向服务端发送删除指令
                $.ajax({
                    type: 'post',
                    url: baseUrl+"gametea/delroom",
                    data:{room:data.roomName},
                    dataType: 'json',
                    success: function(res){
                        if(res.meta.code===200){
                            //加载层
                            var index = layer.load(0, {shade: false,time:1000} ); //0代表加载的风格，支持0-2
                            setTimeout(function(){ layer.msg(''+res.meta.msg+'',{icon:1,time:1000});index.closed}, 500);
                            setTimeout(obj.del(), 1000); //删除对应行（tr）的DOM结构，并更新缓存
                        }else{
                            //加载层
                            var index = layer.load(0, {shade: false,time:1000} ); //0代表加载的风格，支持0-2
                            setTimeout(function(){ layer.msg(''+res.meta.msg+'',{icon:2,time:1000});index.closed}, 1000);
                        }
                    }
                });
            });
        }
    });
    $("#addroom").click(function () {
       var index =  layer.open({
            id: 'insert-form',
            title: "房间的详细信息",
            type: 2
            ,area:  ['45%', '680px']
            // ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
            ,moveType: 1 //拖拽模式，0或者1
            ,content: 'roominfo.html'
            ,btn:["确定","取消"]
            ,yes: function(){
                var frameId=document.getElementsByTagName("iframe")[0].id;
                var marshal = $('#'+frameId)[0].contentWindow.ads();
               marshal = JSON.parse(marshal);
               console.log(marshal)
               $.ajax({
                   type: 'post',
                   url: baseUrl+"club/addroom",
                   data: marshal,
                   success: function(data){
                      location.reload();
                   }
               });
                layer.close(index);
            }
        });

    })
    var $ = layui.$, active = {
        reload: function(){
            var demoReload = $('#demoReload');

            //执行重载
            table.reload('testReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                        roomName: demoReload.val()
                }
            });
        }
    };

    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

})

